{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 动物识别\n",
    "\n",
    "百度AI提供动物识别功能，识别近八千种动物，接口返回动物名称。可以帮助叫出更多动物的名字。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始我们的实验\n",
    "(1)登录百度AI开发平台，选择“图像识别”，使用“创建应用”建立一个动物识别应用。 获取应用的AppID、API Key、Secret Key，注意:Secret Key要先选择显示，然后复制其现实的字符。\n",
    "\n",
    "例如我创建的图像识别应用获取的AppID、API Key、Secret Key如下：\n",
    "\n",
    "    \"\"\" 公开课限定量图像识别，需要更改为自己的api接口 \"\"\"\n",
    "    pic_APP_ID = '21222675'\n",
    "    pic_API_KEY = 'kuuuxPDhItuMo1b4eQgipBAV'\n",
    "    pic_SECRET_KEY = 'ZWzTrLWfQMy6AXT9FU9X5ggHXAfkFU9I'\n",
    "\n",
    "百度AI开发平台注册链接:https://login.bce.baidu.com/?account=&redirect=http%3A%2F%2Fconsole.bce.baidu.com%2F%3Ffromai%3D1#/aip/overview\n",
    "\n",
    "(2)熟悉我封装的百度AI图像识别的模块 在代码中输入如下代码，导入我的封装的模块\n",
    "\n",
    "    import sys\n",
    "    sys.path.append('../baidu_api_lib')\n",
    "    from baidu_picture import baidu_picture_2_msg\n",
    "封装库提供两个方法：初始化方法，图像识别方法。\n",
    "\n",
    "<1>初始化方法，需要传入三个参数，就是上步我们创建“图像识别”应用获取的三个参数 参数信息如下：\n",
    "\n",
    "    \"\"\" 公开课限定量图像识别，需要更改为自己的api接口 \"\"\"\n",
    "    pic_APP_ID = '21222675'\n",
    "    pic_API_KEY = 'kuuuxPDhItuMo1b4eQgipBAV'\n",
    "    pic_SECRET_KEY = 'ZWzTrLWfQMy6AXT9FU9X5ggHXAfkFU9I'\n",
    "    \n",
    "调用方式：\n",
    "\n",
    "    # 传入百度AI的参数，进行图像识别\n",
    "    pic_msg = baidu_picture_2_msg(pic_APP_ID, pic_API_KEY, pic_SECRET_KEY)\n",
    "    初始化完毕后，获取pic_msg对象，操作该对象就能进行动物识别。\n",
    "    \n",
    "<2>动物识别方法 这是我封装百度动物识别方法的函数原型：\n",
    "\n",
    "    #从百度AI获取图片分析结果\n",
    "    response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg')\n",
    "\n",
    "baidu_request_url:百度AI的调用接口，\n",
    "\n",
    "    #百度AI的调用url\n",
    "    baidu_request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "\n",
    "    'read_word.jpg':进行动物识别的图片。\n",
    "    返回值:response为文字识别的信息\n",
    "\n",
    "    response.json()[\"words_result\"]:识别结果数组\n",
    "    response.json()[\"words_result\"][i][\"name\"]:第i个识别结果名称\n",
    "    response.json()[\"words_result\"][i][\"score\"]:第i个识别结果概率\n",
    "    \n",
    "    备注：\n",
    "        response.json()[\"words_result\"][0][\"name\"]为百度AI认为识别最高的动物名称\n",
    "        response.json()[\"words_result\"][0][\"score\"为百度AI认为识别最高的动物概率\n",
    "...还有不少参数，通过print(response.json())可以发现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下小派认识动物的本领,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "from playsound import playsound\n",
    "import cv2 as cv\n",
    "import time\n",
    "from multiprocessing import Process, Queue\n",
    "import multiprocessing\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_picture import baidu_picture_2_msg\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\"\"\" 公开课限定量图像识别，需要更改为自己的api接口 \"\"\"\n",
    "pic_APP_ID = '21222675'\n",
    "pic_API_KEY = 'kuuuxPDhItuMo1b4eQgipBAV'\n",
    "pic_SECRET_KEY = 'ZWzTrLWfQMy6AXT9FU9X5ggHXAfkFU9I'\n",
    "\n",
    "#百度AI的调用url\n",
    "baidu_request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "\n",
    "#　获取摄像头图像\n",
    "def camera_frame_func(task_name, mult_queue1, mydict):\n",
    "    \n",
    "    # 创建一个VideoCapture对象\n",
    "    capture = cv.VideoCapture(0) \n",
    "    \n",
    "    #更新人脸识别信息\n",
    "    mydict[\"get_pic\"] = \"false\"\n",
    "    \n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    try:\n",
    "        while True:\n",
    "            # 一帧一帧读取视频\n",
    "            ret, frame = capture.read()\n",
    "            \n",
    "            #将拍摄到的图片发送到消息队列中\n",
    "            if mydict[\"get_pic\"] == \"true\":\n",
    "                mydict[\"get_pic\"] = \"false\"\n",
    "                mult_queue1.put(frame)\n",
    "\n",
    "            # 本地显示视频图像\n",
    "            cv.imshow('real_time picture', frame) \n",
    "            cv.waitKey(1)\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        # 释放cap,销毁窗口\n",
    "        capture.release()    \n",
    "        #关闭显示窗口\n",
    "        cv.destroyAllWindows() \n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "#处理图像\n",
    "def proc_frame_func(task_name, mult_queue, mydict):\n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    # 传入百度AI的参数，进行图像识别\n",
    "    pic_msg = baidu_picture_2_msg(pic_APP_ID, pic_API_KEY, pic_SECRET_KEY)\n",
    "    \n",
    "    #传入百度AI的参数，进行语音合成\n",
    "    word_2_sound = baidu_word_2_sound(pic_APP_ID, pic_API_KEY, pic_SECRET_KEY)\n",
    "    word_2_sound.trans_word_to_sound(\"咱们比拼下谁认识的动物多，让我们开始吧\",'tst_sound.mp3')\n",
    "    os.system('mplayer ' + 'tst_sound.mp3')\n",
    "    time.sleep(2)\n",
    "    \n",
    "    try:\n",
    "       \n",
    "        while True:\n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"给我3秒时间，让我想想这是什么动物\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(2)\n",
    "            word_2_sound.trans_word_to_sound(\"3\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"2\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "\n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"1\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #关闭显示窗口\n",
    "            cv.destroyAllWindows()            \n",
    "\n",
    "            # 写入图片\n",
    "            cv.imwrite('read_word.jpg',frame)\n",
    "            \n",
    "            #从百度AI获取图片分析结果\n",
    "            response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg')\n",
    "            \n",
    "            #给出百度AI分析的数据\n",
    "            print(response.json())\n",
    "            \n",
    "            #获取分析结果中的动物名称，并语音播报\n",
    "            animal = response.json()['result'][0]['name']\n",
    "            \n",
    "            if animal == \"非动物\":\n",
    "                word_2_sound.trans_word_to_sound(\"你在跟我开玩笑嘛，这不是动物\",'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            else:\n",
    "                word_2_sound.trans_word_to_sound(\"这是\"+animal,'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"我厉害吧，不服就继续比拼\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')    \n",
    "            time.sleep(3)\n",
    "            \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('tst_sound.mp3')\n",
    "        os.remove('read_word.jpg')\n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        \n",
    "        mydict=multiprocessing.Manager().dict()\n",
    "        \n",
    "        #　定义传递图像队列和传递图像处理结果队列\n",
    "        q_frame = Queue()\n",
    "        \n",
    "        #　采集摄像头进程、处理图片进程、播报语音信息\n",
    "        get_camera_frame = Process(target=camera_frame_func, args=(\"获取摄像头图像\", q_frame, mydict))\n",
    "        proc_frame       = Process(target=proc_frame_func, args=(\"处理图像\", q_frame, mydict))\n",
    "        \n",
    "        # 启动任务\n",
    "        get_camera_frame.start()\n",
    "        proc_frame.start()\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 让我们临摹代码，学习“动物识别”功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #导入标准库\n",
    "# import sys\n",
    "\n",
    "# import os\n",
    "\n",
    "# from playsound import playsound\n",
    "\n",
    "# import cv2 as cv\n",
    "\n",
    "# import time\n",
    "\n",
    "# from multiprocessing import Process, Queue\n",
    "\n",
    "# import multiprocessing\n",
    "\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../baidu_api_lib')\n",
    "\n",
    "# from baidu_picture import baidu_picture_2_msg\n",
    "\n",
    "# from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\n",
    "# \"\"\" 公开课限定量图像识别，需要更改为自己的api接口 \"\"\"\n",
    "# pic_APP_ID = '21222675'\n",
    "\n",
    "# pic_API_KEY = 'kuuuxPDhItuMo1b4eQgipBAV'\n",
    "\n",
    "# pic_SECRET_KEY = 'ZWzTrLWfQMy6AXT9FU9X5ggHXAfkFU9I'\n",
    "\n",
    "\n",
    "# #百度AI的调用url\n",
    "# baidu_request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "\n",
    "\n",
    "# #　获取摄像头图像\n",
    "# def camera_frame_func(task_name, mult_queue1, mydict):\n",
    "\n",
    "    \n",
    "#     # 创建一个VideoCapture对象\n",
    "#     capture = cv.VideoCapture(0) \n",
    "\n",
    "    \n",
    "#     #更新人脸识别信息\n",
    "#     mydict[\"get_pic\"] = \"false\"\n",
    "\n",
    "    \n",
    "#     #　给出提示信息\n",
    "#     print(task_name + \"任务启动\")\n",
    "\n",
    "    \n",
    "#     try:\n",
    "\n",
    "#         while True:\n",
    "\n",
    "#             # 一帧一帧读取视频\n",
    "#             ret, frame = capture.read()\n",
    "\n",
    "            \n",
    "#             #将拍摄到的图片发送到消息队列中\n",
    "#             if mydict[\"get_pic\"] == \"true\":\n",
    "\n",
    "#                 mydict[\"get_pic\"] = \"false\"\n",
    "\n",
    "#                 mult_queue1.put(frame)\n",
    "\n",
    "\n",
    "#             # 本地显示视频图像\n",
    "#             cv.imshow('real_time picture', frame) \n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "\n",
    "#     except KeyboardInterrupt:\n",
    "\n",
    "#         # 释放cap,销毁窗口\n",
    "#         capture.release()    \n",
    "\n",
    "#         #关闭显示窗口\n",
    "#         cv.destroyAllWindows() \n",
    "\n",
    "#         print(task_name + \"任务被终止\")\n",
    "\n",
    "        \n",
    "# #处理图像\n",
    "# def proc_frame_func(task_name, mult_queue, mydict):\n",
    "\n",
    "#     #　给出提示信息\n",
    "#     print(task_name + \"任务启动\")\n",
    "\n",
    "    \n",
    "#     # 传入百度AI的参数，进行图像识别\n",
    "#     pic_msg = baidu_picture_2_msg(pic_APP_ID, pic_API_KEY, pic_SECRET_KEY)\n",
    "\n",
    "    \n",
    "#     #传入百度AI的参数，进行语音合成\n",
    "#     word_2_sound = baidu_word_2_sound(pic_APP_ID, pic_API_KEY, pic_SECRET_KEY)\n",
    "\n",
    "#     word_2_sound.trans_word_to_sound(\"咱们比拼下谁认识的动物多，让我们开始吧\",'tst_sound.mp3')\n",
    "\n",
    "#     os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#     time.sleep(2)\n",
    "\n",
    "    \n",
    "#     try:\n",
    "\n",
    "       \n",
    "#         while True:\n",
    "\n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"给我3秒时间，让我想想这是什么动物\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(2)\n",
    "\n",
    "#             word_2_sound.trans_word_to_sound(\"3\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(1)\n",
    "\n",
    "            \n",
    "#             #从队列中获取图片，显示图像质量\n",
    "#             mydict[\"get_pic\"] = \"true\"\n",
    "\n",
    "#             frame = mult_queue.get()\n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "            \n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"2\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(1)\n",
    "\n",
    "\n",
    "#             #从队列中获取图片，显示图像质量\n",
    "#             mydict[\"get_pic\"] = \"true\"\n",
    "\n",
    "#             frame = mult_queue.get()\n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "            \n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"1\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(1)\n",
    "\n",
    "            \n",
    "#             #从队列中获取图片，显示图像质量\n",
    "#             mydict[\"get_pic\"] = \"true\"\n",
    "\n",
    "#             frame = mult_queue.get()\n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "            \n",
    "#             #关闭显示窗口\n",
    "#             cv.destroyAllWindows()        \n",
    "\n",
    "\n",
    "#             # 写入图片\n",
    "#             cv.imwrite('read_word.jpg',frame)\n",
    "\n",
    "            \n",
    "#             #从百度AI获取图片分析结果\n",
    "#             response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg')\n",
    "\n",
    "            \n",
    "#             #给出百度AI分析的数据\n",
    "#             print(response.json())\n",
    "\n",
    "            \n",
    "#             #获取分析结果中的动物名称，并语音播报\n",
    "#             animal = response.json()['result'][0]['name']\n",
    "\n",
    "            \n",
    "#             if animal == \"非动物\":\n",
    "#                 word_2_sound.trans_word_to_sound(\"你在跟我开玩笑嘛，这不是动物\",'tst_sound.mp3')\n",
    "\n",
    "#                 os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             else:\n",
    "#                 word_2_sound.trans_word_to_sound(\"这是\"+animal,'tst_sound.mp3')\n",
    "\n",
    "#                 os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "                \n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"我厉害吧，不服就继续比拼\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')   \n",
    "\n",
    "#             time.sleep(3)\n",
    "\n",
    "            \n",
    "#     except KeyboardInterrupt:\n",
    "#         os.remove('tst_sound.mp3')\n",
    "\n",
    "#         os.remove('read_word.jpg')\n",
    "\n",
    "#         print(task_name + \"任务被终止\")\n",
    "\n",
    "        \n",
    "# if __name__ == \"__main__\":\n",
    "\n",
    "#     try:\n",
    "        \n",
    "#         mydict=multiprocessing.Manager().dict()\n",
    "\n",
    "        \n",
    "#         #　定义传递图像队列和传递图像处理结果队列\n",
    "#         q_frame = Queue()\n",
    "\n",
    "        \n",
    "#         #　采集摄像头进程、处理图片进程、播报语音信息\n",
    "#         get_camera_frame = Process(target=camera_frame_func, args=(\"获取摄像头图像\", q_frame, mydict))\n",
    "\n",
    "#         proc_frame       = Process(target=proc_frame_func, args=(\"处理图像\", q_frame, mydict))\n",
    "\n",
    "        \n",
    "#         # 启动任务\n",
    "#         get_camera_frame.start()\n",
    "\n",
    "#         proc_frame.start()\n",
    "\n",
    "\n",
    "#     except KeyboardInterrupt:\n",
    "\n",
    "#         print(\"任务被终止了\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "1、小派不仅认识动物，还认识植物、菜品、还有各国货币。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下小派认识动物的本领,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "from playsound import playsound\n",
    "import cv2 as cv\n",
    "import time\n",
    "from multiprocessing import Process, Queue\n",
    "import multiprocessing\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_picture import baidu_picture_2_msg\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\"\"\" 公开课限定量图像识别，需要更改为自己的api接口 \"\"\"\n",
    "pic_APP_ID = '21222675'\n",
    "pic_API_KEY = 'kuuuxPDhItuMo1b4eQgipBAV'\n",
    "pic_SECRET_KEY = 'ZWzTrLWfQMy6AXT9FU9X5ggHXAfkFU9I'\n",
    "\n",
    "#百度AI的调用url，货币识别\n",
    "baidu_request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/currency\"\n",
    "\n",
    "#　获取摄像头图像\n",
    "def camera_frame_func(task_name, mult_queue1, mydict):\n",
    "    \n",
    "    # 创建一个VideoCapture对象\n",
    "    capture = cv.VideoCapture(0) \n",
    "    \n",
    "    #更新图片识别信息\n",
    "    mydict[\"get_pic\"] = \"false\"\n",
    "    \n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    try:\n",
    "        while True:\n",
    "            # 一帧一帧读取视频\n",
    "            ret, frame = capture.read()\n",
    "            \n",
    "            #将拍摄到的图片发送到消息队列中\n",
    "            if mydict[\"get_pic\"] == \"true\":\n",
    "                mydict[\"get_pic\"] = \"false\"\n",
    "                mult_queue1.put(frame)\n",
    "\n",
    "            # 本地显示视频图像\n",
    "            cv.imshow('real_time picture', frame) \n",
    "            cv.waitKey(1)\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        # 释放cap,销毁窗口\n",
    "        capture.release()    \n",
    "        #关闭显示窗口\n",
    "        cv.destroyAllWindows() \n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "#处理图像\n",
    "def proc_frame_func(task_name, mult_queue, mydict):\n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    # 传入百度AI的参数，进行图像识别\n",
    "    pic_msg = baidu_picture_2_msg(pic_APP_ID, pic_API_KEY, pic_SECRET_KEY)\n",
    "    \n",
    "    #传入百度AI的参数，进行语音合成\n",
    "    word_2_sound = baidu_word_2_sound(pic_APP_ID, pic_API_KEY, pic_SECRET_KEY)\n",
    "    word_2_sound.trans_word_to_sound(\"咱们比拼下谁认识的多，让我们开始吧\",'tst_sound.mp3')\n",
    "    os.system('mplayer ' + 'tst_sound.mp3')\n",
    "    time.sleep(2)\n",
    "    \n",
    "    try:\n",
    "       \n",
    "        while True:\n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"给我3秒时间，让我想想这是什么\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(2)\n",
    "            word_2_sound.trans_word_to_sound(\"3\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"2\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "\n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"1\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #关闭显示窗口\n",
    "            cv.destroyAllWindows()            \n",
    "\n",
    "            # 写入图片\n",
    "            cv.imwrite('read_word.jpg',frame)\n",
    "            \n",
    "            #从百度AI获取图片分析结果\n",
    "            response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg')\n",
    "            \n",
    "            #给出百度AI分析的数据\n",
    "            print(response.json())\n",
    "            \n",
    "            #获取分析结果中的图像名称，并语音播报\n",
    "            thing = response.json()['result']['hasdetail']\n",
    "            \n",
    "            if thing:\n",
    "                #获取货币名称和面值\n",
    "                currencyName = response.json()['result']['currencyName']\n",
    "                currencyDenomination = response.json()['result']['currencyDenomination']\n",
    "                \n",
    "                #语音播报获取到的信息\n",
    "                word_2_sound.trans_word_to_sound(\"这是\"+currencyName+'面值'+currencyDenomination,'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            else:\n",
    "                word_2_sound.trans_word_to_sound(\"这不是货币，换个真货币\",'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"我厉害吧，不服就继续比拼\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')    \n",
    "            time.sleep(3)\n",
    "            \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('tst_sound.mp3')\n",
    "        os.remove('read_word.jpg')\n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        \n",
    "        mydict=multiprocessing.Manager().dict()\n",
    "        \n",
    "        #　定义传递图像队列和传递图像处理结果队列\n",
    "        q_frame = Queue()\n",
    "        \n",
    "        #　采集摄像头进程、处理图片进程、播报语音信息\n",
    "        get_camera_frame = Process(target=camera_frame_func, args=(\"获取摄像头图像\", q_frame, mydict))\n",
    "        proc_frame       = Process(target=proc_frame_func, args=(\"处理图像\", q_frame, mydict))\n",
    "        \n",
    "        # 启动任务\n",
    "        get_camera_frame.start()\n",
    "        proc_frame.start()\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
